package com.sirdc.modules.sys.service;


import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.exceptions.SystemException;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.dao.SysUserLoginDao;
import com.sirdc.modules.sys.entity.SysRolePermission;
import com.sirdc.modules.sys.entity.SysUserLogin;
import com.sirdc.modules.sys.filter.SysUserLoginFilter;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserLoginService extends StringPKService<SysUserLogin> {

    @Autowired
    private SysUserLoginDao sysUserLoginDao;

    @Autowired
    private SysTableService tableService;

    @Override
    protected void beforeSave(SysUserLogin obj) {
        obj.setSysId(tableService.updateMaxSysId("t_sys_user_login", null));
    }


    @Override
    protected BaseDao<SysUserLogin, String> getDao() {
        return sysUserLoginDao;
    }

    public List<SysUserLogin> queryUserOnlineList(SysUserLoginFilter filter){
        return sysUserLoginDao.queryUserOnlineList(filter);
    }

    public HSSFWorkbook queryUserOnlineCount(){
//查询用户单位、角色
        List<SysUserLogin> onLineList = sysUserLoginDao.queryUserOnlineCount();
        if(onLineList.isEmpty()){
            throw new SystemException("没有数据");
        }
        HSSFWorkbook wb = new HSSFWorkbook();//创建一个对应Excel文件
        HSSFSheet sheet = wb.createSheet("用户登录次数"); //床建excel 中的sheet
        HSSFRow row = sheet.createRow(0);
        sheet.autoSizeColumn(1, true);

        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中格式
        style.setFillForegroundColor((short) 13);// 设置背景色
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //row.setHeight((short) 900);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //------列-------
        CellRangeAddress cellRangeAddress0 = new CellRangeAddress(1, 1, 0, 0);
        sheet.addMergedRegion(cellRangeAddress0);
        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("序号");
        cell1.setCellStyle(style);
        //------列-------
        CellRangeAddress cellRangeAddress1 = new CellRangeAddress(1, 1, 1, 1);
        sheet.addMergedRegion(cellRangeAddress1);
        HSSFCell cell2 = row.createCell(1);
        cell2.setCellValue("姓名");
        cell2.setCellStyle(style);
        sheet.setColumnWidth(1, 15 * 512);

        //------列-------
        CellRangeAddress cellRangeAddress2 = new CellRangeAddress(1, 1, 3, 3);
        sheet.addMergedRegion(cellRangeAddress2);
        HSSFCell cell3 = row.createCell(2);
        cell3.setCellValue("登录次数");
        cell3.setCellStyle(style);

        //------列-------
        CellRangeAddress cellRangeAddress3 = new CellRangeAddress(1, 1, 3, 3);
        sheet.addMergedRegion(cellRangeAddress3);
        HSSFCell cell4 = row.createCell(3);
        cell4.setCellValue("登录排名");
        cell4.setCellStyle(style);

        String name = "";
        int rows = 0;
        //姓名、联系电话
        for (int i = 0; i < onLineList.size(); i++) {
            SysUserLogin online = (SysUserLogin) onLineList.get(i);
            name = online.getName();
            rows ++ ;
            row = sheet.createRow(rows);
            row.createCell(0).setCellValue(rows);
            row.createCell(1).setCellValue(name);
            row.createCell(2).setCellValue(online.getNum().toString());
            row.createCell(3).setCellValue(online.getRownum().toString());
        }
        return wb;
    }
}
